package com.qiche.util;

import java.util.Date;
import java.util.Random;

public class CodeGenerate {
	
	private final static String LETTER = "abcdefghijklmnopqrstuvwxyz";
	private final static String LETTER_AND_NUMBER = "abcdefghijklmnopqrstuvwxyz0123456789";
	private final static String NUMBER = "0123456789";
	/**只生成字母代码*/
	public final static int LETTER_TYPE = 1;
	/**生成字母跟数字混合代码*/
	public final static int LETTER_AND_NUMBER_TYPE = 2;
	/**生成数字代码*/
	public final static int NUMBER_TYPE = 3;

	/**
	 * 生成代码
	 * @param type	类型1 纯数字, 2 英文字母加数字, 3 纯英文字母
	 * @param len 代码长度
	 * @return code
	 */
	public static String generate(int type, int len) {
		String code = null;
		switch (type) {
		case 1:
			code = doGenerate(NUMBER, len);
			break;
		case 2:
			code = doGenerate(LETTER_AND_NUMBER, len);
			break;
		case 3:
			code = doGenerate(LETTER, len);
			break;
		default:
			code = doGenerate(NUMBER, len);
			break;
		}
		return code;
	}

	private static String doGenerate(String srcCode, int len) {
		StringBuilder code = new StringBuilder();
		for (int i = 0; i < len; i++) {
			char e = srcCode.charAt(new Random().nextInt(srcCode.length()));
			code.append(e);
		}
		return code.toString();
	}
	
	public static String orderNumber(){
	 	  String paymentNumber=new Date().getTime()+generate(1,5);
		return paymentNumber;
	}

}
